﻿using Raffles.OA.Data;
using Raffles.OA.Utility;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Raffles.OA.Service
{
    /// <summary>
    /// 信息库业务类
    /// </summary>
    public class BaseItemService : BaseService
    {
        /// <summary>
        /// 报价项单价缓存Key
        /// </summary>
        private static string _quotationRelatePriceCacheKey => "QuotationRelatePrice_Cache";

        /// <summary>
        /// 行程城市缓存Key
        /// </summary>
        private static string _quotationDestinationCacheKey => "QuotationDestination_Cache";

        /// <summary>
        /// 行程城市下拉树
        /// </summary>
        /// <returns></returns>
        public async Task<List<TreeVo>> GetDestinationTree()
        {
            List<TreeVo> treeVos = null;
            //读取缓存
            treeVos = GetQuotationDestinationCache();
            if (treeVos == null)
            {
                treeVos = await _dbContext.Queryable<BaseDestinationEntity>().Where(x => !x.DeletedMark && x.EnabledMark)
                                       .Select(x => new TreeVo { id = x.Id, pId = x.ParentId, name = x.Name })
                                       .ToListAsync();
            }

            return treeVos;
        }

        /// <summary>
        /// 获取行程国家分段
        /// </summary>
        /// <returns></returns>
        public async Task<List<Dictionary<string, object>>> GetProjectSegmentSelect()
        {
            return await _dbContext.Queryable<BaseDestinationEntity>()
                                                                   .Where(x => !x.DeletedMark && x.EnabledMark && x.ParentId == 0)
                                                                   .Select(x => new { Key = x.Id, Value = x.Name })
                                                                   .ToDictionaryListAsync();
        }

        #region 报价价格缓存相关

        /// <summary>
        /// 读取报价相关价格缓存
        /// </summary>
        public List<RelatePriceDto> GetQuotationRelatePriceCache()
        {
            return CacheHelper.GetCache<List<RelatePriceDto>>(_quotationRelatePriceCacheKey);
        }

        /// <summary>
        /// 重载报价相关价格缓存
        /// </summary>
        public async Task LoadQuotationRelatePriceCache()
        {
            List<RelatePriceDto> dtos = new List<RelatePriceDto>();
            //团队住宿
            dtos.AddRange(await _dbContext.Queryable<BaseHotelEntity>().Where(x => !x.DeletedMark && x.EnabledMark).Select(x => new RelatePriceDto { ItemId = x.Id, Code = QuotationItemEnum.Hotel.ToString(), Price = x.Price }).ToListAsync());
            //团队餐饮
            dtos.AddRange(await _dbContext.Queryable<BaseRestaurantEntity>().Where(x => !x.DeletedMark && x.EnabledMark).Select(x => new RelatePriceDto { ItemId = x.Id, Code = QuotationItemEnum.Restaurant.ToString(), Price = x.Price }).ToListAsync());
            //交通导游
            dtos.AddRange(await _dbContext.Queryable<BaseTransportationEntity>().Where(x => !x.DeletedMark && x.EnabledMark).Select(x => new RelatePriceDto { ItemId = x.Id, Code = QuotationItemEnum.Transportation.ToString(), Price = x.Price }).ToListAsync());
            //交通导游
            //景点门票
            dtos.AddRange(await _dbContext.Queryable<BaseScenicAreaEntity>().Where(x => !x.DeletedMark && x.EnabledMark).Select(x => new RelatePriceDto { ItemId = x.Id, Code = QuotationItemEnum.ScenicArea.ToString(), Price = x.Price }).ToListAsync());
            //团组公务
            dtos.AddRange(await _dbContext.Queryable<BaseGroupOfficialEntity>().Where(x => !x.DeletedMark && x.EnabledMark).Select(x => new RelatePriceDto { ItemId = x.Id, Code = QuotationItemEnum.GroupOfficial.ToString(), Price = x.Price }).ToListAsync());
            //团组培训
            dtos.AddRange(await _dbContext.Queryable<BaseGroupActivityEntity>().Where(x => !x.DeletedMark && x.EnabledMark).Select(x => new RelatePriceDto { ItemId = x.Id, Code = QuotationItemEnum.GroupActivity.ToString(), Price = x.Price }).ToListAsync());
            //其他费用
            dtos.AddRange(await _dbContext.Queryable<BaseAdditionEntity>().Where(x => !x.DeletedMark && x.EnabledMark).Select(x => new RelatePriceDto { ItemId = x.Id, Code = QuotationItemEnum.Addition.ToString(), Price = x.Price }).ToListAsync());
            CacheHelper.RemoveCache(_quotationRelatePriceCacheKey);
            CacheHelper.SetCache<List<RelatePriceDto>>(dtos, _quotationRelatePriceCacheKey);
        }

        /// <summary>
        /// 移除报价相关价格缓存
        /// </summary>
        public void RemoveQuotationRelatePriceCache()
        {
            CacheHelper.RemoveCache(_quotationRelatePriceCacheKey);
        }

        #endregion 报价价格缓存相关

        #region 行程城市缓存相关

        /// <summary>
        /// 读取行程城市缓存
        /// </summary>
        public List<TreeVo> GetQuotationDestinationCache()
        {
            return CacheHelper.GetCache<List<TreeVo>>(_quotationDestinationCacheKey);
        }

        /// <summary>
        /// 重载行程城市缓存
        /// </summary>
        public async Task LoadQuotationDestinationCache()
        {
            List<TreeVo> treeVos = new List<TreeVo>(); 
            treeVos.AddRange(await _dbContext.Queryable<BaseDestinationEntity>().Where(x => !x.DeletedMark && x.EnabledMark).Select(x => new TreeVo { id = x.Id, pId = x.ParentId, name = x.Name }).ToListAsync());
            CacheHelper.RemoveCache(_quotationDestinationCacheKey);
            CacheHelper.SetCache<List<TreeVo>>(treeVos, _quotationDestinationCacheKey);
        }

        /// <summary>
        /// 移除行程城市缓存
        /// </summary>
        public void RemoveQuotationDestinationCache()
        {
            CacheHelper.RemoveCache(_quotationDestinationCacheKey);
        }


        #endregion
    }
}